package com.jiaquyun.jcyx.main.mine

import android.os.Bundle
import android.text.Editable
import android.view.View
import androidx.lifecycle.LifecycleOwner
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.jiaquyun.jcyx.R
import com.jiaquyun.jcyx.app.JCApplication
import com.jiaquyun.jcyx.entity.UserInvoiceCreate
import com.jiaquyun.jcyx.jetpack.mode.MineViewModel
import com.module.lemlin.http.Status
import com.module.lemlin.http.msg
import com.module.lemlin.owner.OwnerViewModeActivity
import kotlinx.android.synthetic.main.activity_invoice_submit.*

class InvoiceSubmitActivity : OwnerViewModeActivity<MineViewModel>() {

    companion object {
        private const val KEY_ORDER_PRICE = "KEY_ORDER_PRICE"
        private const val KEY_ORDER_ID_LIST = "KEY_ORDER_ID_LIST"
        fun open(orderPrice: String, orderIds: IntArray) {
            val bundle = Bundle().apply {
                putString(KEY_ORDER_PRICE, orderPrice)
                putIntArray(KEY_ORDER_ID_LIST, orderIds)
            }
            ActivityUtils.startActivity(bundle, InvoiceSubmitActivity::class.java)
        }
    }

    private val mPrice: String? by lazy {
        intent?.extras?.getString(KEY_ORDER_PRICE)
    }

    private val mOrderIds: IntArray? by lazy {
        intent?.extras?.getIntArray(KEY_ORDER_ID_LIST)
    }

    override fun viewModel(): MineViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_invoice_submit

    override fun stateBar(): StatusBar {
        return StatusBar(isLightMode = true, color = R.color.white, view = flInvoiceSubmitBar)
    }

    override fun initView() {

        ivAppBack.setOnClickListener { finish() }

        tvInvoiceSubmitPrice.text = mPrice

        rbInvoiceSubmitType1.isChecked = true

        rbInvoiceSubmitType1.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked) {
                llInvoiceSubmitNo.visibility = View.GONE
                tvInvoiceSubmitNo.text = Editable.Factory.getInstance().newEditable(null)
            }
        }

        rbInvoiceSubmitType2.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked) {
                llInvoiceSubmitNo.visibility = View.VISIBLE
            }
        }

        tvInvoiceSubmitSure.setOnClickListener {
            val title = tvInvoiceSubmitTitle.text.toString()
            val no = tvInvoiceSubmitNo.text.toString()

            if (title.isEmpty()) {
                toast("请输入开票抬头")
                return@setOnClickListener
            }
            if (rbInvoiceSubmitType2.isChecked) {
                if (no.isEmpty()) {
                    toast("请输入纳税识别号")
                    return@setOnClickListener
                }
            }

            val create = UserInvoiceCreate(
                invoice_no = no,
                invoice_title = title,
                order_id = mOrderIds ?: return@setOnClickListener,
                shop_id = JCApplication.userShops?.id ?: 55
            )

            viewModel.userInvoiceCreate(create)


            LogUtils.e("===${GsonUtils.toJson(create)}")
        }
    }

    override fun initViewMode() {
        viewModel.userInvoiceCreateLiveDate.observe(this as LifecycleOwner, { response ->
            if (response.status == Status.FAILURE) {
                toast(response.error?.msg)
            }
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            toast(response.data?.msg ?: "提交成功")
            finish()
        })
    }
}